﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Sgr.Admin.WebHost.Infrastructure.Migrations.Sgr
{
    /// <inheritdoc />
    public partial class InitialSgrSchema : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterDatabase()
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_administrative_division",
                columns: table => new
                {
                    sgr_id = table.Column<long>(type: "bigint", nullable: false, comment: "主键"),
                    m_code = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false, comment: "行政区划代码")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_name = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "行政区划名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_level = table.Column<int>(type: "int", nullable: false, comment: "行政区划级别：0-未知，1-国家，2-省，3-市，4-区县，5-乡镇街道"),
                    m_shortname = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true, comment: "行政区划简称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_pinyinname = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: true, comment: "拼音名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_pinyinabbr = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: true, comment: "拼音缩写")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_description = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: true, comment: "行政区划描述")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_state = table.Column<int>(type: "int", nullable: false, comment: "状态：0-启用，1-禁用"),
                    m_ordernumber = table.Column<int>(type: "int", nullable: false, defaultValue: 0, comment: "排序号"),
                    m_sgr_parentid = table.Column<long>(type: "bigint", nullable: false, comment: "上级行政区划ID"),
                    m_sgr_nodepath = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: false, comment: "节点路径")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sgr_rowversion = table.Column<long>(type: "bigint", nullable: false, comment: "行版本"),
                    sgr_creatoruserid = table.Column<long>(type: "bigint", nullable: false, comment: "创建的用户ID"),
                    sgr_creationtime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间"),
                    sgr_lastmodifieruserid = table.Column<long>(type: "bigint", nullable: false, comment: "修改的用户ID"),
                    sgr_lastmodificationtime = table.Column<DateTime>(type: "datetime(6)", nullable: true, comment: "修改时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_administrative_division", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_datacategoryitem",
                columns: table => new
                {
                    sgr_id = table.Column<long>(type: "bigint", nullable: false, comment: "主键"),
                    m_categorytypecode = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "字典分类标识")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_dcitemname = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "字典项名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_dcitemvalue = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "字典项值")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_remarks = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "备注")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_iseditable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否可编辑"),
                    m_ordernumber = table.Column<int>(type: "int", nullable: false, comment: "排序号"),
                    m_state = table.Column<int>(type: "int", nullable: false, comment: "状态"),
                    sgr_parentid = table.Column<long>(type: "bigint", nullable: false, comment: "上级节点Id"),
                    sgr_nodepath = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: false, comment: "树节点层次目录")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sgr_rowversion = table.Column<long>(type: "bigint", nullable: false, comment: "行版本"),
                    sgr_creatoruserid = table.Column<long>(type: "bigint", nullable: false, comment: "创建的用户ID"),
                    sgr_creationtime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间"),
                    sgr_lastmodifieruserid = table.Column<long>(type: "bigint", nullable: false, comment: "修改的用户ID"),
                    sgr_lastmodificationtime = table.Column<DateTime>(type: "datetime(6)", nullable: true, comment: "修改时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_datacategoryitem", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_logoperate",
                columns: table => new
                {
                    sgr_id = table.Column<long>(type: "bigint", nullable: false, comment: "主键"),
                    m_loginname = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "登录账号")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_displayname = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "用户显示名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_ipaddress = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "登录Ip地址")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_location = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true, comment: "登录地点")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_clientbrowser = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "客户端浏览器")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_clientos = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "客户端系统")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_operateway = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: true, comment: "登录途径")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_requestdescription = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: false, comment: "请求说明")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_requesturl = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: false, comment: "请求地址")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_httpmethod = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false, comment: "请求方法")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_requestparam = table.Column<string>(type: "varchar(4000)", maxLength: 4000, nullable: false, comment: "请求参数")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_requesttime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "请求时间"),
                    m_requestduration = table.Column<long>(type: "bigint", nullable: true, comment: "请求耗时"),
                    m_status = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "请求结果"),
                    m_remark = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true, comment: "请求结果描述")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sgr_orgid = table.Column<long>(type: "bigint", nullable: false, comment: "所在组织ID"),
                    sgr_creatoruserid = table.Column<long>(type: "bigint", nullable: false, comment: "创建的用户ID"),
                    sgr_creationtime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_logoperate", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_message_trackerlog",
                columns: table => new
                {
                    sgr_id = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_processed_on_utc = table.Column<DateTime>(type: "datetime(6)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_message_trackerlog", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_omnimetric",
                columns: table => new
                {
                    sgr_id = table.Column<long>(type: "bigint", nullable: false, comment: "主键"),
                    m_metrickey = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "指标唯一标识")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_timegranularity = table.Column<int>(type: "int", nullable: false, comment: "统计粒度"),
                    m_timevalueutc = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "统计时间点"),
                    m_basicvalue01 = table.Column<double>(type: "double", nullable: false, comment: "基础数值01"),
                    m_basicvalue02 = table.Column<double>(type: "double", nullable: false, comment: "基础数值02"),
                    m_basicvalue03 = table.Column<double>(type: "double", nullable: false, comment: "基础数值03"),
                    m_extendedvalues = table.Column<string>(type: "json", nullable: false, comment: "扩展数值（JSON序列化）")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    m_updatetimeutc = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "指标值更新时间"),
                    sgr_rowversion = table.Column<long>(type: "bigint", nullable: false, comment: "行版本")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_omnimetric", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "sgr_outbox_message",
                columns: table => new
                {
                    sgr_id = table.Column<long>(type: "bigint", nullable: false, comment: "主键"),
                    sqr_topic = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false, comment: "消息主题")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sqr_type = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false, comment: "消息类型")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sqr_message_id = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false, comment: "消息唯一标识")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sqr_payload = table.Column<string>(type: "json", nullable: false, comment: "消息载荷（JSON）")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    sqr_occurred_on_utc = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "发生时间（UTC）"),
                    sqr_next_on_utc = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "下一次尝试投递时间（UTC）"),
                    sqr_status = table.Column<int>(type: "int", nullable: false, comment: "消息状态"),
                    sqr_dispatched_on_utc = table.Column<DateTime>(type: "datetime(6)", nullable: true, comment: "实际投递时间（UTC），为空表示未投递"),
                    sqr_retry_count = table.Column<int>(type: "int", nullable: false, comment: "重试次数"),
                    sqr_last_error = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "上次投递失败错误信息")
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sgr_outbox_message", x => x.sgr_id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_AdminDiv_Name",
                table: "sgr_administrative_division",
                column: "m_name");

            migrationBuilder.CreateIndex(
                name: "IX_sgr_administrative_division_m_sgr_parentid",
                table: "sgr_administrative_division",
                column: "m_sgr_parentid");

            migrationBuilder.CreateIndex(
                name: "UK_AdminDiv_Code",
                table: "sgr_administrative_division",
                column: "m_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sgr_datacategoryitem_m_categorytypecode",
                table: "sgr_datacategoryitem",
                column: "m_categorytypecode");

            migrationBuilder.CreateIndex(
                name: "IX_sgr_datacategoryitem_m_categorytypecode_m_dcitemvalue",
                table: "sgr_datacategoryitem",
                columns: new[] { "m_categorytypecode", "m_dcitemvalue" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_LogOperate_RequestTime_OrgId",
                table: "sgr_logoperate",
                columns: new[] { "m_requesttime", "sgr_orgid" });

            migrationBuilder.CreateIndex(
                name: "IX_OmniMetric_Key_Granularity",
                table: "sgr_omnimetric",
                columns: new[] { "m_metrickey", "m_timegranularity" });

            migrationBuilder.CreateIndex(
                name: "UK_OmniMetric_Key_Granularity_TimeValue",
                table: "sgr_omnimetric",
                columns: new[] { "m_metrickey", "m_timegranularity", "m_timevalueutc" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_outbox_message_status_next_on_utc",
                table: "sgr_outbox_message",
                columns: new[] { "sqr_status", "sqr_next_on_utc" });

            migrationBuilder.CreateIndex(
                name: "ux_outbox_message_message_id",
                table: "sgr_outbox_message",
                column: "sqr_message_id",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "sgr_administrative_division");

            migrationBuilder.DropTable(
                name: "sgr_datacategoryitem");

            migrationBuilder.DropTable(
                name: "sgr_logoperate");

            migrationBuilder.DropTable(
                name: "sgr_message_trackerlog");

            migrationBuilder.DropTable(
                name: "sgr_omnimetric");

            migrationBuilder.DropTable(
                name: "sgr_outbox_message");
        }
    }
}
